package com.fanco.learn_datastruct_leetcode.designpattern.iterator;

import java.util.Iterator;

/**
 * @author qiaowang 2022/2/22
 * 描述
 */
public class ComputerColleageIterator implements Iterator {

    // 这里我们需要知道Department是以怎样的方式 存放的
    Department[] departments;
    int position = 0; // 遍历的位置

    public ComputerColleageIterator(Department[] departments) {
        this.departments = departments;
    }

    /**
     * Returns {@code true} if the iteration has more elements.
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     *
     * @return {@code true} if the iteration has more elements
     */
    @Override
    public boolean hasNext() {
        if (position >= departments.length || departments[position] == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration
     * @throws NoSuchElementException if the iteration has no more elements
     */
    @Override
    public Object next() {
        Department department = departments[position];
        position += 1;
        return department;
    }

    // 删除的方法我们默认空实现
    @Override
    public void remove() {

    }
}
